package com.xiaoyu.lanling.feature.moment.data.detail

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.base.utils.ListUtil
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.moment.detail.CommentListEvent
import com.xiaoyu.lanling.feature.moment.model.detail.CommentItem

class CommentDetailListData(val feedId:String) : PagedListDataModelWithRequestTag<CommentItem, Any>() {

    val tag = Any()

    override fun doQueryData() {
        RequestCreator.create<CommentListEvent>(CommentListEvent::class.java)
                .setRequestUrl(HttpConfig.LANLING_MOMENT_COMMENT_LIST)
                .addQueryData("fid",feedId)
                .appendQueryDataTo(listPageInfo)
                .setRequestHandler(object : RequestDefaultHandler<CommentListEvent, JsonData>() {
                    override fun processOriginData(originData: JsonData): CommentListEvent {
                        val rawData = originData.optJson("data")
                        val list = ListUtil.convertToList(rawData.toList()){
                            CommentItem(it)
                        }
                        updateResult(rawData,list)
                        return CommentListEvent(requestTag)
                    }

                    override fun onRequestFinish(data: CommentListEvent) {
                        data.post()
                    }
                })
                .enqueue()
    }

    override fun getRequestTag(): Any {
        return tag
    }
}